import React,{useState,useEffect} from 'react'
import {notification, Table,Button} from 'antd'
import { CheckOutlined, CloseOutlined} from '@ant-design/icons';
import { getArticle,updateAuditStateArticle} from "../../../api/api"
export default function Audit(props) {
  const [dataSource, setDataSource] = useState([]);
  const {id,roleId,region,username}=JSON.parse(localStorage.getItem("token"))
  const columns = [
    {
      title: '新闻标题',
      dataIndex: 'title',
      render: (title, item) => {
        return <a href={`#/news-manage/preview/${item.id}`}>{title}</a>
      }
    },
    {
      title: '作者',
      dataIndex: 'nickname',
    },
    {
      title: '新闻分类',
      dataIndex: 'classifyName'
    },
    {
      title: 'VIP文章',
      dataIndex: 'vip',
      render: (vip) => {
        return vip?'是':'否'
      }
    },
    {
      title: '操作',
      dataIndex: 'publishState',
      render: (publishState,item) => {
        return (
          <div>
          <Button type="primary" shape="circle" size='large' icon={<CheckOutlined />} onClick={()=>{
              setDataSource(dataSource.filter(data=>data.id!==item.id))
              //修改文章审核状态
              updateAuditStateArticle({
                id:item.id,
                "auditState": 2,
                "publishState":1
              })
              notification.info({
                message: '通知',
                description: `作者【${item.nickname}】的新闻【${item.title}】已通过`,
                placement: "bottomRight",
              });
            }} />
            <Button type="primary" danger shape="circle" size='large' icon={<CloseOutlined />} onClick={()=>{
                setDataSource(dataSource.filter(data=>data.id!==item.id))
                //修改文章审核状态
                updateAuditStateArticle({
                  id:item.id,
                  "auditState": 3,
                })
                notification.info({
                  message: '通知',
                  description: `作者【${item.nickname}】的新闻【${item.title}】已驳回`,
                  placement: "bottomRight",
                });
              }} />
          </div>
        )
      }
    },
  ]
  useEffect(() => {
    const httpRequest = async () => {
      const roleObj={
        "1":"superadmin",
        "2":"admin",
        "3":"editor",
      }
      //获取文章
      const res=await getArticle({
        id,
        auditState:1
      })
      setDataSource(roleObj[roleId]==="superadmin"?res.data.data:[
        ...res.data.data.filter(item=>item.author===username),
        ...res.data.data.filter(item=>item.region===region && roleObj[item.roleId]==="editor")
      ])
    }
    httpRequest()
  }, [id,username,roleId,region])
  return (
    <div>
      <Table dataSource={dataSource} columns={columns} rowKey={(item) => item.id} pagination={{
        pageSize: 5,
        pageSizeOptions:[]
      }} />
    </div>
  )
}
